package com.bsoft.gol.editor.dao;


import ctd.persistence.annotation.DAOMethod;
import ctd.persistence.annotation.DAOParam;
import ctd.persistence.support.hibernate.template.AbstractHibernateStatelessResultAction;
import ctd.persistence.support.hibernate.template.HibernateSessionTemplate;
import ctd.persistence.support.hibernate.template.HibernateStatelessResultAction;
import com.bsoft.gol.editor.editor.EditorDict;
import com.bsoft.gol.editor.editor.qo.EditorDictQo;
import org.apache.commons.lang3.StringUtils;
import org.hibernate.SQLQuery;
import org.hibernate.StatelessSession;
import org.hibernate.transform.Transformers;
import com.bsoft.gol.editor.utils.CommonUtils;

import java.util.List;

/**
 * Created by Sangs on 2020/4/3.
 */
public abstract class EditorDictDAO extends ctd.persistence.support.hibernate.HibernateSupportDelegateDAO<EditorDict> {

    public EditorDictDAO() {
        this.setKeyField("dictId");
    }


    /**
     * 查询列表
     * @return
     */
    public  List<EditorDict> queryEditorDictInfoList(final EditorDictQo qo) {
        HibernateStatelessResultAction<List<EditorDict>> action = new AbstractHibernateStatelessResultAction<List<EditorDict>>() {
            @Override
            public void execute(StatelessSession ss) throws Exception {
                StringBuffer hsql = new StringBuffer();

                hsql.append("select create_id createId,create_time createTime,dict_address dictAddress,dict_code dictCode,dict_id  dictId,dict_leaf_node_flag dictLeafNodeFlag,dict_name  dictName,dict_param dictParam,dict_state dictState,dict_type dictType,parent_id parentId from editor_dict a where 1=1");
                if(qo.getDictCode() != null && CommonUtils.isNotEmpty(qo.getDictCode())) {
                    hsql.append(" and a.dict_code ='"+qo.getDictCode()+"'");
                }
                if(qo.getDictName() != null && CommonUtils.isNotEmpty(qo.getDictName())) {
                    hsql.append(" and a.dict_name='"+qo.getDictName()+"'");
                } if(qo.getDictType() != null && CommonUtils.isNotEmpty(qo.getDictType())) {
                    hsql.append(" and a.dict_type='"+qo.getDictType()+"'");
                } if(qo.getParentId() != null && CommonUtils.isNotEmpty(qo.getParentId())) {
                    hsql.append(" and a.parent_id='"+qo.getParentId()+"'");
                } if(qo.getDictState() != null && CommonUtils.isNotEmpty(qo.getDictState())) {
                    hsql.append(" and a.dict_stat='"+qo.getDictState()+"'");
                } if(qo.getDictLeafNodeFlag() != null && CommonUtils.isNotEmpty(qo.getDictLeafNodeFlag())) {
                    hsql.append(" and a.dict_leaf_node_flag='"+qo.getDictLeafNodeFlag()+"'");
                }
                hsql.append("  order by  create_time  ");

                SQLQuery sqlQuery = ss.createSQLQuery(hsql.toString());
                sqlQuery = (SQLQuery) sqlQuery.setResultTransformer(Transformers.ALIAS_TO_ENTITY_MAP);

                List<EditorDict> list = sqlQuery.list();
                setResult(list);

            }
        };
        HibernateSessionTemplate.instance().execute(action);
        return action.getResult();
    }

    public  List<EditorDict> queryEditorDictInfoListBycode(final EditorDictQo qo) {
        HibernateStatelessResultAction<List<EditorDict>> action = new AbstractHibernateStatelessResultAction<List<EditorDict>>() {
            @Override
            public void execute(StatelessSession ss) throws Exception {
                StringBuffer hsql = new StringBuffer();

                hsql.append("select dict_id  dictId,dict_name  dictName from editor_dict a where 1=1");
                if(qo.getDictCode() != null && CommonUtils.isNotEmpty(qo.getDictCode())) {
                    hsql.append(" and parent_id=(SELECT dict_id from editor_dict where dict_code='"+qo.getDictCode()+"')");
                }

                hsql.append("  order by  create_time  ");

                SQLQuery sqlQuery = ss.createSQLQuery(hsql.toString());
                sqlQuery = (SQLQuery) sqlQuery.setResultTransformer(Transformers.ALIAS_TO_ENTITY_MAP);

                List<EditorDict> list = sqlQuery.list();
                setResult(list);

            }
        };
        HibernateSessionTemplate.instance().execute(action);
        return action.getResult();
    }


    @DAOMethod(sql = "update EditorDict set dictId=:dictId where dictId =:dictId ")
    public abstract void updateEditorDictState(@DAOParam("dictId") String dictId,@DAOParam("dictState") Byte dictState);

    @DAOMethod(sql = "from EditorDict where parentId =:dictId ")
    public abstract List<EditorDict> queryEditorDictByParentId(@DAOParam("dictId") String dictId);

}